﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Text.RegularExpressions;
namespace IRCTest
{
    public class IRCActions : IRCBasic
    {
        private String Nick;
        private String channel2;
        public String Channel2 { get { return channel2; } set { channel2 = value; } }

        public IRCActions(ref StreamReader Reader, ref StreamWriter Writing, String Chan1, String Chan2, String Nick)
        {
            IRCBasic.Reader = Reader;
            IRCBasic.Writer = Writing;

            this.Channel = Chan1;
            this.Channel2 = Chan2;

            this.Nick = Nick;
        }



        public void Identify(String Password)
        {
            
            Send_Message3(Password,"IDENTIFY","NickServ");
        }
        public void Complete_reg(String Code)
        {
            Send_Message3(Code, "CONFIRM", "NickServ");
        }

        public void Kill_Ghost(String Nick, String Pass)
        {
            Send_Message3(String.Concat(Nick, " ", Pass), "Ghost", "NickServ");
        }
        public void Ask_inv(String Channel)
        {
            Send_Message3(Channel, "invite", "ChanServ");
        }
        public void Register(String Email, String Password)
        {
            Send_Message3(String.Concat(Password," ", Email ), "REGISTER", "NickServ");
        }

        public static String getUser(String Line)
        {
            if (Line.IndexOf("!") == -1)
                return String.Empty;
            return Line.Substring(1, Line.IndexOf("!") - 1);
        }


        public static String getReceipient(String Line)
        {
            try
            {
                
                Regex Pat1 = new Regex(@"(?<=^:[A-z\.\s0-9=@!-]+)#[A-Za-z\.]+(?=\s:)");

                Regex Pat2 = new Regex(@"(?<=^:[A-z\.\s0-9=@!-]+:?)#[A-Za-z\.]+");
                Regex PrivMsgPat= new Regex(@"(?<=^:[A-z\.\s0-9=@!-]+PRIVMSG\s)[A-z]+");
                Match Receipient = Pat1.Match(Line);
                Match Rec_list_pat = Pat2.Match(Line);
                if (Receipient.Success)
                {
                    return Receipient.Value.Trim();
                }

                else if (Rec_list_pat.Success)
                    return Rec_list_pat.Value.Trim();
                else if (PrivMsgPat.IsMatch(Line))
                    return PrivMsgPat.Match(Line).Value.Trim();
                else
                    return String.Empty;
            }

            catch 
            {
                return String.Empty;
            }
        }
        public static String getMessage(String Line)
        {
            String[] Result = new String[Line.Split(':').Length];
            Result = Line.Split(':');
            Regex Pat = new Regex(@"(?<=[A-z]+\s#?[A-z\.]+\s:).+");
            Regex Pat2 = new Regex(@"(?<=^:[A-z\.\s0-9#?=@]+:).+");
      
            Match Message = Pat.Match(Line);
            Match Values = Pat2.Match(Line);
            if (Message.Success)
            {
                return Message.Value.Trim();
            }
            else if (Values.Success)
                return Values.Value.Trim();
            else
                return String.Empty;
            // Regex Pat = new Regex(@"\s:![A-Za-z^(\s#)]+[^@](?=[A-z0-9\s])");
        }

        public static bool getCode(String Line,out String Message)
        {
            
            Message = String.Empty;
            Regex Pat = new Regex(@"(?<=:[A-Za-z\.]+\s)[0-9]{3}");
            Match Code = Pat.Match(Line);
            if (Code.Success)
            {
                Message = Code.Value.Trim();
                return Code.Success;
            }
            else
                return Code.Success;
        }

        public static String getNewNick(String Line)
        {
            Regex NickPat = new Regex(@"(?<=^:[A-Za-z!@0-9\.-]+\sNICK\s)[A-Za-z0-9]+");
            Match Match = NickPat.Match(Line);
            if (Match.Success)
                return Match.Value.Trim();
            else
                return String.Empty;
        }

    }
}
